package com.itheima.controller.backend.security;


import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.dto.CheckGroupDTO;
import com.itheima.pojo.vo.CheckGroupVO;
import com.itheima.service.CheckGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 检查组管理相关接口
 */
@Slf4j
@RestController
@RequestMapping("/checkgroup")
@Api(tags = "检查组管理")
public class CheckGroupController {
	@Autowired
	private CheckGroupService checkGroupService;

	@PostMapping("/add")
	@ApiOperation("新增检查组")
	public Result add(@RequestBody CheckGroupDTO checkGroupDTO, String checkItemIds) {
		try {
			checkGroupService.add(checkGroupDTO, checkItemIds);
			log.info("新增检查组成功");
			return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
		} catch (Exception e) {
			log.info("新增检查组失败");
			return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
		}

	}

	@GetMapping("/delete")
	@ApiOperation("根据ID删除检查组")
	public Result delete(@RequestParam("id") String checkGroupId) {
		try {
			checkGroupService.delete(checkGroupId);
			log.info("删除检查组成功");
			return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
		} catch (Exception e) {
			log.info("删除检查组失败");
			return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
		}

	}

	@PostMapping("/edit")
	@ApiOperation("编辑检查组")
	public Result edit(@RequestBody CheckGroupDTO checkGroupDTO, String checkItemIds) {
		try {
			checkGroupService.edit(checkGroupDTO, checkItemIds);
			log.info("编辑检查组成功");
			return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
		} catch (Exception e) {
			log.info("编辑检查组失败");
			return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
		}

	}

	@PostMapping("/findPage")
	@ApiOperation("条件分页查询(项目编码或项目名称）")
	public PageResult findPage(@RequestBody QueryPageBean checkGroupDTO) {
		try {
			PageResult pageResult = checkGroupService.findPage(checkGroupDTO);
			log.info("检查组分页查询检查组成功");
			return new PageResult(pageResult.getTotal(), pageResult.getRows());
		} catch (Exception e) {
			log.info("检查组分页查询检查组失败");
			return new PageResult(null, null);
		}

	}

	@GetMapping("/findById")
	@ApiOperation("根据ID查询检查组")
	public Result findById(@RequestParam("id") String checkGroupId) {
		try {
			CheckGroupDTO checkGroupDTO = checkGroupService.findById(checkGroupId);
			log.info("根据ID查询检查组成功");
			return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroupDTO);
		} catch (Exception e) {
			log.info("根据ID查询检查组失败");
			return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
		}
	}

	@GetMapping("/findAll")
	@ApiOperation("查询所有检查组")
	public Result findAll() {
		try {
			List<CheckGroupVO> List = checkGroupService.findAll();
			log.info("查询所有检查组成功");
			return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, List);
		} catch (Exception e) {
			log.info("查询所有检查组失败");
			return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
		}
	}

	@GetMapping("/findCheckGroupIdsBySetmealId")
	@ApiOperation("根据套餐ID查询检查组ID")
	public Result findCheckGroupIdsBySetmealId(Integer setmealId) {
		try {
			List<Integer> List = checkGroupService.findCheckGroupIdsBySetmealId(setmealId);
			log.info("根据套餐ID查询检查组ID成功");
			return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, List);
		} catch (Exception e) {
			log.info("根据套餐ID查询检查组ID失败");
			return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
		}
	}
}
